package com.android.happymall.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;

import com.android.happymall.R;
import com.android.happymall.app.BaseActivity;
import com.android.happymall.fragment.Fragment2;
import com.android.happymall.fragment.detail.Fragment_Detail_DirectBuy_GoodsDetail_Comment;
import com.android.happymall.fragment.detail.Fragment_Detail_GoodsDetail_Comment;
import com.android.happymall.fragment.detail.Fragment_Detail_GoodsDetail_DetailWeb;
import com.android.happymall.fragment.detail.Fragment_DirectBuy_FirmOrder;
import com.android.happymall.fragment.detail.Fragment_Order_Address;
import com.android.happymall.fragment.detail.Fragment_Store_Info;
import com.android.happymall.fragment.group.Fragment_BidHistory;
import com.android.happymall.fragment.group.Fragment_DirectBuy;
import com.android.happymall.fragment.group.Fragment_HistoryTrendingChart;
import com.android.happymall.fragment.group.Fragment_Main_Type_Result;
import com.android.happymall.fragment.group.Fragment_Main_WebView;
import com.android.happymall.fragment.group.Fragment_OrderToShare;
import com.android.happymall.fragment.group.Fragment_VoucherPay;
import com.android.happymall.fragment.group.Fragment_group_BrandNation_Sort;
import com.android.happymall.fragment.member.Fragment_FeedBack;
import com.android.happymall.fragment.member.Fragment_Mine_AboutUs;
import com.android.happymall.fragment.member.Fragment_Mine_Address;
import com.android.happymall.fragment.member.Fragment_Mine_Coin_Detail;
import com.android.happymall.fragment.member.Fragment_Mine_CommonProblem;
import com.android.happymall.fragment.member.Fragment_Mine_Customer;
import com.android.happymall.fragment.member.Fragment_Mine_Detail;
import com.android.happymall.fragment.member.Fragment_Mine_DirectBuy_Order_Detail;
import com.android.happymall.fragment.member.Fragment_Mine_Express;
import com.android.happymall.fragment.member.Fragment_Mine_Fans;
import com.android.happymall.fragment.member.Fragment_Mine_Invitation;
import com.android.happymall.fragment.member.Fragment_Mine_Invitation_List;
import com.android.happymall.fragment.member.Fragment_Mine_Message;
import com.android.happymall.fragment.member.Fragment_Mine_Money;
import com.android.happymall.fragment.member.Fragment_Mine_Money_Cash;
import com.android.happymall.fragment.member.Fragment_Mine_Rank;
import com.android.happymall.fragment.member.Fragment_Mine_Rate;
import com.android.happymall.fragment.member.Fragment_Mine_Score_Detail;
import com.android.happymall.fragment.member.Fragment_Mine_TeamDetail;
import com.android.happymall.fragment.member.Fragment_Setting;
import com.android.happymall.fragment.member.Fragment_Use_MerVoucher;
import com.android.happymall.utils.ConstantsUrl;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BaseObject extends BaseActivity implements Fragment_Main_WebView.CallBackValue {


    @BindView(R.id.main_top_bg)
    Toolbar mainTopBg;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_object);
        ButterKnife.bind(this);
        title = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE);
        initTitle(0, title);
        if(savedInstanceState==null){
            initUI();
        }
    }


    private void initUI() {
        String need_id = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_NEED_ID);
        FragmentManager fm = getSupportFragmentManager();
        switch (title) {
            case "意见反馈":
                fm.beginTransaction().replace(R.id.frament, Fragment_FeedBack.newInstance()).commit();
                break;
            case "设置":
                fm.beginTransaction().replace(R.id.frament, Fragment_Setting.newInstance()).commit();
                break;
//            case "充值":
//                fm.beginTransaction().replace(R.id.frament, Fragment_OrderToShare.newInstance()).commit();
//                break;
            case "使用商家券":
                fm.beginTransaction().replace(R.id.frament, Fragment_Use_MerVoucher.newInstance(need_id)).commit();
                break;
            case "晒单":
                String order_thumbs = getIntent().getStringExtra("order_thumbs");
                String order_title = getIntent().getStringExtra("order_title");
                String order_price = getIntent().getStringExtra("order_price");
                fm.beginTransaction().replace(R.id.frament, Fragment_OrderToShare.newInstance(need_id,order_thumbs,order_title,order_price)).commit();
                break;
            case "购劵支付":
                String singleprice = getIntent().getStringExtra("singleprice");
                String currentprice = getIntent().getStringExtra("currentprice");
                String currentnumber = getIntent().getStringExtra("currentnumber");
                String currentcoin = getIntent().getStringExtra("currentcoin");
                String currentsummary = getIntent().getStringExtra("currentsummary");
                String currenttitle = getIntent().getStringExtra("currenttitle");
                String currentthumb = getIntent().getStringExtra("currentthumb");
                String currentstime = getIntent().getStringExtra("currentstime");
                String currentetime = getIntent().getStringExtra("currentetime");
                fm.beginTransaction().replace(R.id.frament, Fragment_VoucherPay.newInstance(need_id,singleprice,currentprice,
                        currentnumber,currentcoin,currenttitle,currentsummary,currentthumb,currentstime,currentetime)).commit();
                break;
            case "出价记录":
                String order_sn = getIntent().getStringExtra("order_sn");
                fm.beginTransaction().replace(R.id.frament, Fragment_BidHistory.newInstance(order_sn)).commit();
                break;
            case "历史成交走势图":
                String detail_htc = getIntent().getStringExtra("detail_htc");
                fm.beginTransaction().replace(R.id.frament, Fragment_HistoryTrendingChart.newInstance(detail_htc)).commit();
                break;
            case "商品详情":
                String detail_goodsid = getIntent().getStringExtra("detail_goodsid");
                fm.beginTransaction().replace(R.id.frament, Fragment_Detail_GoodsDetail_DetailWeb.newInstance(detail_goodsid)).commit();
                break;
//            case "新手指南":
//                String web_url1 = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_WEB_URL);
//                fm.beginTransaction().replace(R.id.frament, Fragment_NewerGuideWeb.newInstance(web_url1,"")).commit();
//                break;
            case "客服":
                mainTopBg.setVisibility(View.GONE);
                String web_url = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_WEB_URL);
                String web_content = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_WEB_CONTENT);
                fm.beginTransaction().replace(R.id.frament, Fragment_Main_WebView.newInstance(web_url, web_content)).commit();
                break;
            case "最新成交":
                fm.beginTransaction().replace(R.id.frament, Fragment2.newInstance()).commit();
                break;
            case "品牌团":
                fm.beginTransaction().replace(R.id.frament, Fragment_group_BrandNation_Sort.newInstance(0)).commit();
                break;
            case "海淘团":
                fm.beginTransaction().replace(R.id.frament, Fragment_group_BrandNation_Sort.newInstance(1)).commit();
                break;
            case "店铺详情":
                fm.beginTransaction().replace(R.id.frament, Fragment_Store_Info.newInstance(need_id)).commit();
                break;
            case "试用结果":
                fm.beginTransaction().replace(R.id.frament, Fragment_Main_Type_Result.newInstance(need_id)).commit();
                break;
            case "用户评价":
                fm.beginTransaction().replace(R.id.frament, Fragment_Detail_GoodsDetail_Comment.newInstance(need_id)).commit();
                break;
            case "大市场":
                fm.beginTransaction().replace(R.id.frament, Fragment_DirectBuy.newInstance()).commit();
                break;
            case "确认订单":
                String detailid = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_DETAIL_ID);
                int qty = getIntent().getIntExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_PAY_QTY, 1);
                fm.beginTransaction().replace(R.id.frament, Fragment_DirectBuy_FirmOrder.newInstance(detailid,qty)).commit();
                break;
            case "选择收货地址":
                fm.beginTransaction().replace(R.id.frament, Fragment_Order_Address.newInstance()).commit();
                break;
            case "商品评价":
                fm.beginTransaction().replace(R.id.frament, Fragment_Detail_DirectBuy_GoodsDetail_Comment.newInstance(need_id)).commit();
                break;

            //个人中心
            case "收货地址":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Address.newInstance(need_id)).commit();
                break;
            case "我的佣金":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Money.newInstance(0)).commit();
                break;
            case "提现记录":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Money.newInstance(1)).commit();
                break;
            case "提现":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Money_Cash.newInstance(need_id)).commit();
                break;
            case "我的抽奖":
                // fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Lottery.newInstance()).commit();
                break;
            case "我的消息":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Message.newInstance("")).commit();
                break;
            case "评价":
                String good_id = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_RATE_GOOD_ID);
                String id = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_RATE_ID);
                String buy_num = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_RATE_BUY_NUM);
                String thumb = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_RATE_THUMB);
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Rate.newInstance(thumb, buy_num, good_id, id)).commit();
                break;
            case "物流详情":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Express.newInstance(need_id)).commit();
                break;
            case "订单详情":
                int type = getIntent().getIntExtra("type",0);
                if(type == 1){
                    fm.beginTransaction().replace(R.id.frament, Fragment_Mine_DirectBuy_Order_Detail.newInstance(need_id)).commit();
                }else{
                    fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Detail.newInstance(need_id)).commit();
                }
                break;
            case "团详情":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_TeamDetail.newInstance(need_id)).commit();
                mainTopBg.setVisibility(View.GONE);
                break;
            case "拼团王":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Rank.newInstance()).commit();
                break;
            case "我的粉丝":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Fans.newInstance()).commit();
                break;
            case "关于我们":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_AboutUs.newInstance()).commit();
                break;
            case "积分明细":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Score_Detail.newInstance()).commit();
                break;
            case "客服中心":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Customer.newInstance()).commit();
                break;
            case "常见问题":
                String title = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_PROBLEM_TITLE);
                String content = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_PROBLEM_CONTENT);
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_CommonProblem.newInstance(title,content)).commit();
                break;
            case "邀请好友":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Invitation.newInstance()).commit();
                break;
            case "我的邀请":
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Invitation_List.newInstance()).commit();
                break;
            case "余额明细":
                String coin = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_COIN_NUM);
                String free_coin = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_FREE_COIN_NUM);
                fm.beginTransaction().replace(R.id.frament, Fragment_Mine_Coin_Detail.newInstance(coin,free_coin)).commit();
                break;


        }

    }

    private WebView web;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (title.equals("")) {
            if (keyCode == KeyEvent.KEYCODE_BACK && web.canGoBack()) {
                web.goBack();// 返回前一个页面
                return true;
            }
        }
        finish();
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void SendMessageValue(WebView webview) {
        this.web = webview;
    }


}
